﻿using B.S.BasicData.Domain.Li;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfaces;
using B.S.BasicData.Write.Api.Application.Command.L;
using MediatR;

namespace B.S.BasicData.Write.Api.Application.CommandHandler.L
{
    public class ProductManageShowCommandHandler : IRequestHandler<ProductManageShowCommand, ApiPaging<ProductManage>>
    {
        private readonly IBaseRepository<ProductManage> _productmanagebaseRepository;
        public ProductManageShowCommandHandler(IBaseRepository<ProductManage> productmanagebaseRepository)
        {
            _productmanagebaseRepository = productmanagebaseRepository;
        }

        public Task<ApiPaging<ProductManage>> Handle(ProductManageShowCommand request, CancellationToken cancellationToken)
        {
           ApiPaging<ProductManage>result = new ApiPaging<ProductManage>();
            var productmanage = _productmanagebaseRepository.GetAll();

            if (!string.IsNullOrEmpty(request.ProductName))
            {
                productmanage = productmanage.Where(a => a.ProductName.Contains(request.ProductName));
            }
            result.TotalCount = productmanage.Count();
            result.TotalPage = (int)Math.Ceiling(productmanage.Count() * 1.0 / request.PageSize);
            result.Items = productmanage.OrderByDescending(a => a.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return Task.FromResult(result);
        }
    }
}
